//
//  BDMyGoldViewController.m
//  iOS_beboy
//
//  Created by Chuz NetTech on 17/11/1.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyGoldViewController.h"
#import "BDUserModel.h"
#import <UShareUI/UShareUI.h>
@interface BDMyGoldViewController ()

@end

@implementation BDMyGoldViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = ASLocalizedString(@"我要现金");
    
    NSString *url = [NSString stringWithFormat:@"%@inviteCode", KBDBaseURL];
    NSString *headValid = [BDUserModel getHeaderValidation];
    RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
    [signal subscribeNext:^(id  _Nullable x) {
        [self createrUI:[x[@"code"] integerValue]];
    }];
    [signal subscribeError:^(NSError * _Nullable error) {
        NSLog(@"%ld",error.code);
    }];
}

-(void)createrUI:(NSInteger )code{
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight)];
    backImageView.image = [UIImage imageNamed:@"goldbeijing"];
    backImageView.userInteractionEnabled = YES;
    [self.view addSubview:backImageView];
    
    UIImageView *wantMoneyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, 160)];
    wantMoneyImageView.image = [UIImage imageNamed:@"goldFriend"];
    [backImageView addSubview:wantMoneyImageView];
    
    UILabel *inviteLabel = [[UILabel alloc] init];
    inviteLabel.top = wantMoneyImageView.bottom + 28;
    inviteLabel.left = 40;
    inviteLabel.size = CGSizeMake(kBDScreenWidth - 80, 50);
    inviteLabel.text = [NSString stringWithFormat:ASLocalizedString(@"邀请码：%ld"),code];
    inviteLabel.font = [UIFont systemFontOfSize:20];
    inviteLabel.textColor = [UIColor colorWithHexString:@"c07f41"];
    inviteLabel.backgroundColor = [UIColor colorWithRed:7 / 255.0 green:2 / 255.0  blue:33 / 255.0 alpha:0.5];
    inviteLabel.layer.cornerRadius = 5;
    inviteLabel.textAlignment = NSTextAlignmentCenter;
    [backImageView addSubview:inviteLabel];
    
    UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    inviteButton.top = inviteLabel.bottom + 5;
    inviteButton.left = 40;
    inviteButton.size = CGSizeMake(kBDScreenWidth - 80, 50);
    [inviteButton setTitle:ASLocalizedString(@"立刻邀请")forState:UIControlStateNormal];
    inviteButton.titleLabel.font = [UIFont systemFontOfSize:20];
    inviteButton.layer.cornerRadius = 5;
    inviteButton.backgroundColor = [UIColor colorWithRed:79 / 255.0 green:80 / 255.0 blue:171 / 255.0 alpha:0.5];
    [backImageView addSubview:inviteButton];
    [[inviteButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_Qzone)]];
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            // 根据获取的platformType确定所选平台进行下一步操作
            NSString *url = [NSString stringWithFormat:@"%@tool/shareData", KBDBaseURL];
            NSString *headValid = [BDUserModel getHeaderValidation];
            RACSignal *signal = [[BDNetworkingServer yy_GET:url parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
            
            [signal subscribeNext:^(id  _Nullable x) {
                //创建分享消息对象
                UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
                
                //创建网页内容对象
                NSString *thumbURL = self.shareUrl;
                NSString *title = ASLocalizedString(@"【夜城】邀请好友；立得现金，快来邀请吧！");
                NSString *descr = inviteLabel.text;
                NSString *webUrl = @"http://beboy.me/lead";
                UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title
                                                                                         descr:descr
                                                                                     thumImage:thumbURL];
                //设置网页地址
                shareObject.webpageUrl = webUrl;
                
                //分享消息对象设置分享内容对象
                messageObject.shareObject = shareObject;
                
                //调用分享接口
                [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
                    if (error) {
                        UMSocialLogInfo(@"************Share fail with error %@*********",error);
                    }else{
                        if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                            //                            UMSocialShareResponse *resp = data;
                            //                            //分享结果消息
                            //                            UMSocialLogInfo(@"response message is %@",resp.message);
                            //                            //第三方原始返回的数据
                            //                            UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                            //                            NSLog(ASLocalizedString(@"分享成功了"));
//                            NSString *backUrl = [NSString stringWithFormat:@"%@post/%@/shared", KBDBaseURL, @(model.my_id)];
//                            RACSignal *backSignal = [[BDNetworkingServer yy_POST:backUrl parameters:nil validation:headValid automaticProcessingError:YES] execute:nil];
//                            [backSignal subscribeNext:^(id  _Nullable x) {
//                                model.sharedCount++;
//                                [self.tableView reloadData];
                                [MBManager showBriefAlert:ASLocalizedString(@"分享成功")clickHidden:NO clickBlock:nil];
//                            }];
//                            [backSignal subscribeError:^(NSError * _Nullable error) {
//                                [MBManager showBriefAlert:ASLocalizedString(@"分享失败")clickHidden:NO clickBlock:nil];
//                            }];
                        }else{
                            //                            UMSocialLogInfo(@"response data is %@",data);
                            [MBManager showBriefAlert:ASLocalizedString(@"分享失败")clickHidden:NO clickBlock:nil];
                        }
                    }
                }];
            }];
            [signal subscribeError:^(NSError * _Nullable error) {
                [MBManager showBriefAlert:ASLocalizedString(@"获取分享信息失败")clickHidden:NO clickBlock:nil];
            }];
        }];
    }];
    
    UIImageView *leftImageView = [[UIImageView alloc] init];
    leftImageView.top = inviteButton.bottom + 28;
    leftImageView.left = 0;
    leftImageView.size = CGSizeMake((kBDScreenWidth - 80) / 2, 2);
    leftImageView.image = [UIImage imageNamed:@"zuo"];
    [backImageView addSubview:leftImageView];
    
    UIImageView *rightImageView = [[UIImageView alloc] init];
    rightImageView.top = inviteButton.bottom + 28;
    rightImageView.size = CGSizeMake((kBDScreenWidth - 80) / 2, 2);
    rightImageView.right = kBDScreenWidth;
    rightImageView.image = [UIImage imageNamed:@"goldyou"];
    [backImageView addSubview:rightImageView];
    
    UILabel *ruleLabel = [[UILabel alloc] init];
    ruleLabel.left = leftImageView.right;
    ruleLabel.top = inviteButton.bottom + 22;
    ruleLabel.size = CGSizeMake(80, 12);
    ruleLabel.text = ASLocalizedString(@"活动规则");
    ruleLabel.textColor = [UIColor colorWithHexString:@"c07f41"];
    ruleLabel.font = [UIFont systemFontOfSize:12];
    ruleLabel.textAlignment = NSTextAlignmentCenter;
    [backImageView addSubview:ruleLabel];
    
    UILabel *firstLabel  = [[UILabel alloc] init];
    firstLabel.left = 18;
    firstLabel.top = ruleLabel.bottom + 20;
    firstLabel.width = kBDScreenWidth - 36;
    firstLabel.numberOfLines = 0;
    firstLabel.font = [UIFont systemFontOfSize:12];
    firstLabel.textColor = [UIColor colorWithHexString:@"c9c9c9"];
    firstLabel.text = ASLocalizedString(@"1.凡APP用户可以通过分享功能邀请新用户，邀请者和被邀请者都可以获得10元现金的奖励。\n\n2.被邀请者成功登陆APP，并在个人设置页面添写邀请码（初次个人设置时，个人设置界面添加邀请码填写栏），提交之后，可立刻获得10元现金，添加到账号余额上；被邀请者成就等级达到“新手上路”之后，邀请者可以获得10元现金，添加到账户余额上。\n\n3.余额上的资金可用于APP论坛、商城消费、商家消费抵扣、也可直接体现。");
    [firstLabel sizeToFit];
    [backImageView addSubview:firstLabel];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
